// pages/userInfoCenter/index.js
const App = getApp();
const {Dialog, Toast} = require("../../../../../config/modal")
const utils = require("../../../../../utils/util")
Page({
    /**
     * 页面的初始数据
     */
    data: {
        offsetTop: App.globalData.window.navHeight,
        theamTextColor: App.globalData.theamTextColor,
        capsuleBarHeight: App.$getMenuButtonHeight(),
        loadMore: {
            show: false,
            type: "loading",
            text: "加载中..."
        },
        // 轮播
        swiperImgs: [{
            url: "/static/img/integral-side.jpeg",
            title: ''
        }],
        integral: {
            list: [],
            activeKey: 0,
            goodsType: [],
            type: {
                IN: "IN",
                OUT: "OUT"
            }
        },
        page: {
            pageNum: 1,
            pageSize: 15,
            pages: 0,
        },
        searchParams: '',
    },
    URL: {
        getList: "/integral/goods/getList",
        getGoodsType: "/integral/goods/getGoodsType"
    },
    page: {
        progress: "/pages/tabbar/user/integral/progress/index",
        GoodsDetail: "/pages/tabbar/user/integral/exchange/goods-detail/index",
        records:"/pages/tabbar/user/integral/exchange/records/index",
        target:"/pages/tabbar/user/integral/exchange/index",
        home:"/pages/tabbar/index/index",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        App.$handlerInviteCode(options);
        this.initIntegralGoodsType();
        utils.saveFromPage(App, getCurrentPages());
    },

    onShow() {
    },

    // 获取积分商品
    initIntegralGoodsType() {
        App.$get(true, this.URL.getGoodsType, {}, res => {
            if (res.R) {
                this.setData({
                    'integral.goodsType': res.data,
                    'integral.activeKey': res.data[0],
                })
                this.setData({
                    'searchParams': res.data[0].typeNo
                })
                this.initIntegralGoods(this.data.page.pageNum, false);
            }
        })
    },
    initIntegralGoods(pageNum, loadMore) {
        let params = {
            pageNum: pageNum,
            pageSize: this.data.page.pageSize,
            typeNo: this.data.searchParams
        }
        this.setLoadingMore(loadMore, "loading", "加载中...")
        App.$get(!loadMore ? true : loadMore, this.URL.getList, params, res => {
            this.setData({
                "loadMore.show": false,
            })
            if (res.R) {
                this.setData({
                    "page.pageNum": res.data.pageNum,
                    "page.pageSize": res.data.pageSize,
                    "page.pages": res.data.pages
                })
                if (!loadMore) {
                    this.setData({
                        "integral.list": res.data.rows,
                    })
                    return;
                }
                let sourceData = this.data.integral.list;
                let newData = res.data.rows;
                newData.forEach(data => {
                    sourceData.push(data);
                })
                this.setData({
                    "integral.list": sourceData,
                })
            }
        })
    },
    nowBuy(e) {
        let item = e.currentTarget.dataset.item;
        if (item.stock === 0) {
            Toast("商品库存不足");
            return;
        }
        if (item.status === "下架") {
            Toast("当前商品已下架");
            return;
        }
        App.$navigateTo(this.page.GoodsDetail + "?goodsNo=" + item.goodsNo, true)
    },
    onGoodTypeChange(event) {
        let index = event.detail.index;
        let data = this.data.integral.goodsType;
        this.setData({
            'searchParams': data[index].typeNo,
            'integral.activeKey': index,
            'page.pageNum': 1,
        })
        this.initIntegralGoods(1, false);
    },

    // 点击纸飞机进入兑换中心
    btnPlaneClick(){
        App.$navigateTo(this.page.records,true);
    },
    goBack() {
        let pages = getCurrentPages();
        let prePage = utils.getFromPage(App, pages);
        App.$removeStore((pages[pages.length - 1]).route);
        App.$redirectTo(prePage, false);
    },
    goHome(){
        App.$switchTab(this.page.home,false)
    },
    showLagerImg(e) {
        let imgUrl = e.currentTarget.dataset.img;
        wx.previewImage({
            current: imgUrl,
            urls: [imgUrl]
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.page.pages === 0 || this.data.page.pageNum + 1 > this.data.page.pages) {
            this.setLoadingMore(true, "end", "我是用底线的~")
            return;
        }
        this.initIntegralGoods(this.data.page.pageNum + 1, true);
    },
    setLoadingMore(show, type, text) {
        this.setData({
            "loadMore.show": show,
            "loadMore.type": type,
            "loadMore.text": text,
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return App.$onShare(this.page.target,null,"积分商品")
    },

})
